package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.goods.entity.goods.GoodsGallery;
import com.hoshiicloud.goods.entity.spu.BaseGoodsGallery;
import com.hoshiicloud.goods.entity.spu.ExtSpuTags;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.goods.GoodsGalleryMapper;
import com.hoshiicloud.goods.service.GoodsGalleryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 基础SKU商品图片资源 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class GoodsGalleryServiceImpl extends ServiceImpl<GoodsGalleryMapper, GoodsGallery> implements GoodsGalleryService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "新增商品图片资源表")
    public GoodsGallery saveGoodsGallery(GoodsGallery entity) {

        // 生成主键ID
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return entity;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "更新商品图片资源表失败")
    public boolean updateGoodsGalleryById(GoodsGallery entity) {

        // 更新数据
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "商品管理-商品图片资源删除")
    public boolean deleteGoodsGalleryById(Long id) {

        // 根据id查询数据
        GoodsGallery entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("商品图片资源删除失败，无法找到相关信息");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    /**
     *  根据自定义条件查询图片资源信息
     */
    @Override
    @Cacheable(cacheNames = "GoodsGallery",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public List<GoodsGallery> listGoodsGalleryByWrapper(QueryWrapper wrapper, String key) {
        List list = baseMapper.selectList(wrapper);
        if (list.isEmpty()) {
            throw new ServiceException(CommonError.error("信息查询失败"));
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "批量保存商品图片资源信息")
    @Override
    public Boolean saveGoodsGalleryBatch(List<GoodsGallery> galleryList) {

        boolean isSave = this.saveBatch(galleryList);
        if (!isSave) {
            throw new ServiceException("保存商品图片资源信息失败");
        }

        return true;
    }

    @Override
    public List<GoodsGallery> listBySpuIds(List<Long> spuIds) {
        QueryWrapper<GoodsGallery> q = new QueryWrapper<>();
        q.lambda().in(GoodsGallery::getExtSpuId,spuIds).eq(GoodsGallery::getDeleted,0);
        List<GoodsGallery> goodsGalleries = baseMapper.selectList(q);
        System.out.println("输出："+goodsGalleries.size()+":"+goodsGalleries.get(0));
        return goodsGalleries;
    }

    /**
     * 根据SpuId批量删除
     */
    @Override
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    public Boolean deleteBatchs(Long extSpuId) {
        QueryWrapper<GoodsGallery> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GoodsGallery::getExtSpuId, extSpuId).eq(GoodsGallery::getDeleted, 0);

        GoodsGallery update = new GoodsGallery();
        update.setDeleted(1);

        baseMapper.update(update, wrapper);
        return true;
    }

    /**
     * 批量删除: 物理删除
     */
    @Override
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "批量删除: 物理删除")
    public Boolean removeBatch(List<OmnipotentWrapper> param) {
        QueryWrapper<GoodsGallery> wrapper = new OmnipotentWrapper<GoodsGallery>().getWrapper(param);
        return this.remove(wrapper);
    }

    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "根据自定义条件批量更新")
    @Override
    public boolean updateBatchByWrapper(UpdateBatchParam<GoodsGallery> updateBatchParam) {
        GoodsGallery updateEntity = updateBatchParam.getUpdateEntity();
        QueryWrapper<GoodsGallery> wrapper = updateBatchParam.getWrapper();
        return this.update(updateEntity, wrapper);
    }

    /**
     * 批量更新
     */
    @CacheEvict(cacheNames = "GoodsGallery", allEntries = true)
    @Logable(authName = "批量更新")
    @Override
    public Boolean updateBatch(OmnipotentParam<GoodsGallery> param) {
        OmnipotentWrapper<GoodsGallery> wrapper = new OmnipotentWrapper<>();
        UpdateWrapper<GoodsGallery> updateWrapper = wrapper.getUpdateWrapper(param.getWrappers());
        GoodsGallery entity = param.getEntity();
        return this.update(entity, updateWrapper);
    }
}
